import { defineStore } from 'pinia';
import { ref } from "vue";
export const useGlobalDataStore = defineStore('globalData', () => {
	const globalData = ref({
		isLoggedIn: false,				// 初始化为未登录状态
		VISIT_DATA: {},					// 访问信息
		SYSTEM_INFO: {},				// 系统设备信息
		ENCRYPTED_PWD_INFO: {},			// 加密密码信息
		USER_INFO: {
		  gfid: '0',
		  nickName: '游客',
		  avatar:'/static/login/logo.png',
		  account: '请先登录',
		  phoneNumber: '请先登录',
		  real_state_name: '未认证',
		},
	});
	
	//登录信息
	function logIn(datas){
		//console.log('logIn',Object.prototype.toString.call(datas),datas);
		globalData.value.isLoggedIn = true;

		globalData.value.VISIT_DATA.visit_id=datas.login_sign_id;
		globalData.value.VISIT_DATA.visit_key=datas.login_sign_key;
		globalData.value.VISIT_DATA.visit_str=datas.login_str;

		globalData.value.ENCRYPTED_PWD_INFO.AES_KEY = datas.login_sign_key.slice(0,16);
		globalData.value.ENCRYPTED_PWD_INFO.AES_IV = datas.login_sign_key.slice(-16);


		globalData.value.USER_INFO.gfid=datas.uid;
		globalData.value.USER_INFO.account=datas.account;
		globalData.value.USER_INFO.nickName=datas.nc;
		globalData.value.USER_INFO.avatar=datas.tx;

		//console.log('logIn.real_info',Object.prototype.toString.call(datas.real_info),datas.real_info);
		if(datas.hasOwnProperty("real_info")){
			globalData.value.USER_INFO.real_state_name=datas.real_info.passed_name;
			globalData.value.USER_INFO.phoneNumber=datas.real_info.datas.security_phone;
		}
		
		console.log("logIn: ", globalData.value);
	}
	
	function updateGlobalData(newData){
		globalData.value = newData;
	}
	
	return { 
		globalData,
		logIn,
		updateGlobalData
	};
});